<?php

function setbettype1_0($lotto_id,$gametype,$bettype,$subid,$numArr,$autoRateArr,$RateConfig_5h,$SRate_5h,$betname_5h,$fieldArr,$addVal)
{
    $fieldArr['method'] = 6;
    $num = current($numArr);
    switch ( $bettype )
    {
        case 1 :
            if ( $num == "1" )
            {
                $numRateArr = array("01","03","05","07","09","11","13","15","17",
                    "19");
            }
            else if ( $num == "2" )
            {
                $numRateArr = array("02","04","06","08","10","12","14","16","18",
                    "20");
            }
            else
            {
                return false;
            }
            break;
        case 2 :
            if ( $num == "1" )
            {
                $numRateArr = array("11","12","13","14","15","16","17","18","19",
                    "20");
            }
            else if ( $num == "2" )
            {
                $numRateArr = array("01","02","03","04","05","06","07","08","09",
                    "10");
            }
            else
            {
                return false;
            }
            break;
        case 3 :
            if ( $num == "1" )
            {
                $numRateArr = array("05","06","07","08","09","15","16","17","18",
                    "19");
            }
            else if ( $num == "2" )
            {
                $numRateArr = array("01","02","03","04","10","11","12","13","14",
                    "20");
            }
            else
            {
                return false;
            }
            break;
        case 4 :
            if ( $num == "1" )
            {
                $numRateArr = array("01","03","05","07","09","10","12","14","16",
                    "18");
            }
            else if ( $num == "2" )
            {
                $numRateArr = array("02","04","06","08","11","13","15","17","19",
                    "20");
            }
            else
            {
                return false;
            }
            break;
        case 5 :
            if ( 20 < $gametype )
            {
                if ( $num == "1" )
                {
                    $numRateArr = array("01","02","03","04","05","06","07");
                    break;
                }
                else if ( $num == "2" )
                {
                    $numRateArr = array("08","09","10","11","12","13","14");
                    break;
                }
                else if ( $num == "3" )
                {
                    $numRateArr = array("15","16","17","18","19","20");
                    break;
                }
                else
                {
                    return false;
                }
            }
            else if ( $num == "1" )
            {
                $numRateArr = array("01","04","07","10","13","16","19");
                break;
            }
            else if ( $num == "2" )
            {
                $numRateArr = array("02","05","08","11","14","17","20");
                break;
            }
            else if ( $num == "3" )
            {
                $numRateArr = array("03","06","09","12","15","18","21");
                break;
            }
            else
            {
                return false;
            }
            break;
        case 6 :
            if ( 20 < $gametype )
            {
                if ( $num == "1" )
                {
                    $numRateArr = array("01","05","09","13","17");
                }
                else if ( $num == "2" )
                {
                    $numRateArr = array("02","06","10","14","18");
                }
                else if ( $num == "3" )
                {
                    $numRateArr = array("03","07","11","15","19");
                }
                else if ( $num == "4" )
                {
                    $numRateArr = array("04","08","12","16","20");
                }
                else
                {
                    return false;
                }
            }
            else if ( $num == "1" )
            {
                $numRateArr = array("01","02","03","04","05");
            }
            else if ( $num == "2" )
            {
                $numRateArr = array("06","07","08","09","10");
            }
            else if ( $num == "3" )
            {
                $numRateArr = array("11","12","13","14","15");
            }
            else if ( $num == "4" )
            {
                $numRateArr = array("16","17","18","19","20");
            }
            else
            {
                return false;
            }
            break;
        default :
            return false;
    }
    $val = 0;
    $valIdx = "{$val}_{$subid}";
    if ( ! isset($autoRateArr[$val]) || $autoRateArr[$val]['money'] <= 0 )
    {
        return;
    }
    $rateArr = $RateConfig_5h->setRateArrDir($numRateArr,$addVal);
    $rateOldArr = array();
    $rateNewArr = array();
    $rateOldArr[0] = $SRate_5h->getRates($lotto_id,0,1,$val,$subid);
    if ( 0 < count($rateArr) )
    {
        $okFlag = $SRate_5h->updateRateByMltVal($lotto_id,$val,$subid,$rateArr);
    }
    $rateNewArr[0] = $SRate_5h->getRates($lotto_id,0,1,$val,$subid);
    foreach ( $numRateArr as $newVal )
    {
        $newKey = "r" . $newVal;
        foreach ( $rateNewArr as $rateNewArrKey=> $rateNewArrVal )
        {
            $rateOld = $rateOldArr[$rateNewArrKey];
            $rateNew = $rateNewArr[$rateNewArrKey];
            if ( $rateOld[$newKey] != $rateNew[$newKey] )
            {
                $fieldArr['betcontent'] = "{$RateConfig_5h->xyidTitle[$rateNewArrKey]}[{$betname_5h[$valIdx]}]" . $RateConfig_5h->getBetContent($val,$newVal);
                $fieldArr['orate'] = $rateOld[$newKey];
                $fieldArr['nrate'] = $rateNew[$newKey];
                setRateRecord($fieldArr);
            }
        }
    }
}

if ( ! defined("IN_OP") )
{
    exit("You can't access this file directly...");
}
//if ( $curruser['prev_id'] != 0 )
//{
//    exit();
//}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chkGametype = chkGametype($gametype);
if ( $chkGametype != 3 )
{
    exit("error");
}
$chk = new CheckMode_3d($curruser['acl'],5,$gametype);
$chk->checkShow3dPage();
$betname_5h = judge_type($gametype);
unset($chk);
if ( $curruser['acl'] != "255" && $curruser['acl'] != "300" )
{
    exit();
}
require( "class_5h/SRate_5h.php" );
require( "class_5h/RateConfig_5h.php" );
require( "class_5h/AutoRateConfig_5h.php" );
$conf = new Config();
$SRate_5h = new SRate_5h($gametype);
$RateConfig_5h = new RateConfig_5h($gametype);
$AutoRateConfig_5h = new AutoRateConfig_5h($betname_5h,$g_autorate_5h[$gametype],$gametype);
$_8hFlag = 20 < $gametype ? true : false;
$tbl = $g_gametype_3d[$gametype]['en'];
$disk = "a";
$playtype = getvar("playtype");
$order = "0";
$split = "1";
$ratemod = "0";
$lotto_id = intval(getvar("lotto_id_3d"));
$bettype = getvar("actionBettype");
$numberStr = getvar("actionNum");
$mp = getvar("actionMP");
$addVal = floatval(getvar("addVal"));
$addValType = 1;
$addType1Val = floatval(getvar("addType1Val"));
$method = intval(getvar("method"));
$pagename = intval(getvar("pagename"));
if ( $mp == "m" )
{
    $addVal = 0 - $addVal;
}
$errorMsg = "";
if ( $curruser['enabled'] == 0 || $curruser['enabled'] == 2 )
{
    exit();
}
else if ( $playtype == "" || ! isset($RateConfig_5h->playGroupArr02[$playtype]) )
{
    echo "playtype:{$playtype}";
    exit();
}
$bettypeArr = explode(",",$bettype);
$numArr = explode(",",$numberStr);
$bettype1_0 = intval($conf->get("bettype1_0_{$tbl}"));
$chkBet1_0Arr = array(1,2,3,4,5,6);
$xyMode = intval($conf->get("5hxyMode"));
$bettype1_0_5h = intval($conf->get("bettype1_0_{$tbl}"));
$autoRateArr = $AutoRateConfig_5h->getAutoRateAll();
$disChange = array();
if ( $addValType == 0 )
{
    $addVal = $addType1Val;
}
$sql = "SELECT round, open FROM lotto_{$tbl} WHERE id = {$lotto_id} LIMIT 1";
$rs = getdb($sql);
if ( $rs->eof )
{
    exit();
}
$fieldArr = array("method"=>$method,"gametype"=>$gametype,"round"=>$rs->f['round'],
    "lotto_id"=>$lotto_id,"betcontent"=>"","orate"=>"","nrate"=>"","pagename"=>"0",
    "paperopen"=>$rs->f['open']);
$rs->close();
$okFlag = true;
$ratePec = 1;
$chkSynBallFlag = 20 < $gametype ? intval(getvar("chkSynBall18")) : 0;
foreach ( $bettypeArr as $valStr )
{
    $valArr = explode("_",$valStr);
    if ( count($valArr) != 2 )
    {
        $okFlag = false;
        break;
    }
    $val = intval($valArr[0]);
    $mysubid = intval($valArr[1]);
    if ( ! $RateConfig_5h->chkBetSubXyVal($val,$mysubid) )
    {
        $okFlag = false;
        break;
    }
    if ( ! in_array($val,$RateConfig_5h->playGroupArr02[$playtype]) )
    {
        $okFlag = false;
        break;
    }
    if ( ! isset($autoRateArr[$val]) || $autoRateArr[$val]['money'] <= 0 )
    {
        array_push($disChange,$val);
        continue;
    }
    $betTypeNumberArr = $RateConfig_5h->getBetTypeNumber($val);
    if ( $gametype == 24 && in_array($val,array(1,2,3,4,6)) )
    {
        foreach ( $betTypeNumberArr as $key=> $value )
        {
            $betTypeNumberArr[$key] = (int)$value;
        }
    }
    $numRateArr = array_intersect($betTypeNumberArr,$numArr);
    $rateArr = $RateConfig_5h->setRateArrDir($numRateArr,$addVal);
    if ( $chkSynBallFlag )
    {
        $subidArr = array(1,2,3,4,5,6,7,8);
        $fieldArr['method'] = "1{$method}07";
    }
    else
    {
        $subidArr = array($mysubid);
        $fieldArr['method'] = $method;
    }
    foreach ( $subidArr as $subid )
    {
        $valIdx = "{$val}_{$subid}";
        $rateOldArr = array();
        $rateNewArr = array();
        if ( in_array($bettype,$g_betXYArr[$gametype]) )
        {
            $rateOldArr[1] = $SRate_5h->getRates($lotto_id,1,1,$val,$subid);
            $rateOldArr[2] = $SRate_5h->getRates($lotto_id,2,1,$val,$subid);
        }
        else
        {
            $rateOldArr[0] = $SRate_5h->getRates($lotto_id,0,1,$val,$subid);
        }
        if ( 0 < count($rateArr) )
        {
            $okFlag = $SRate_5h->updateRateByAddVal($lotto_id,$val,$subid,$rateArr);
        }
        if ( in_array($bettype,$g_betXYArr[$gametype]) )
        {
            $rateNewArr[1] = $SRate_5h->getRates($lotto_id,1,1,$val,$subid);
            if ( $xyMode )
            {
                $rateNewArr[2] = $SRate_5h->getRates($lotto_id,2,1,$val,$subid);
            }
        }
        else
        {
            $rateNewArr[0] = $SRate_5h->getRates($lotto_id,0,1,$val,$subid);
        }
        $bettype1_0Rate = 0 - 1;
        foreach ( $numRateArr as $newKey=> $newVal )
        {
            foreach ( $rateNewArr as $rateNewArrKey=> $rateNewArrVal )
            {
                $rateOld = $rateOldArr[$rateNewArrKey];
                $rateNew = $rateNewArr[$rateNewArrKey];
                if ( $rateOld[$newKey] != $rateNew[$newKey] )
                {
                    $fieldArr['betcontent'] = "{$RateConfig_5h->xyidTitle[$rateNewArrKey]}[{$betname_5h[$valIdx]}]" . $RateConfig_5h->getBetContent($val,$newVal);
                    $fieldArr['orate'] = $rateOld[$newKey];
                    $fieldArr['nrate'] = $rateNew[$newKey];
                    setRateRecord($fieldArr);
                }
                if ( $rateOld[$newKey] != 0 )
                {
                    $bettype1_0Rate = $rateNew[$newKey] / $rateOld[$newKey];
                }
                else
                {
                    $bettype1_0Rate = 1;
                }
            }
        }
    }
    if ( $bettype1_0 && in_array($val,$chkBet1_0Arr) && count($numRateArr) == 1 && 0 < $bettype1_0Rate )
    {
        setbettype1_0($lotto_id,$gametype,$val,$mysubid,$numRateArr,$autoRateArr,$RateConfig_5h,$SRate_5h,$betname_5h,$fieldArr,$bettype1_0Rate);
    }
}
if ( $okFlag )
{
    $disChangeStr = 0 < count($disChange) ? "\"" . implode("\",\"",$disChange) . "\"" : "";
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type: text/plain");
    echo "{";
    echo "\"msg\":\"OK\"";
    echo ",\"disChange\":[" . $disChangeStr . "]";
    echo ",\"chgMsg1\":[]";
    echo ",\"chgMsg2\":[]";
    echo "}";
}
else
{
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type: text/plain");
    echo "{\"msg\":\"error\"}";
}
exit();